﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using Common;

namespace Model
{
   public class CodeFactaryDAO
    {
    
       public void SaveConfig(XmlDocument doc ,TreeList tree)
       {
           XmlNode xnRoot=doc.SelectSingleNode("//Code");
           xnRoot.RemoveAll();
           foreach (TreeListNode tlnFolder in tree.Nodes)
           {
               XmlElement xnFolder = doc.CreateElement(enumCodeShopType.Folder.ToString());
               xnFolder.SetAttribute("name",tlnFolder.GetValue("Name").ToString());

               foreach (TreeListNode tlnMethod in tlnFolder.Nodes)
               {
                   XmlElement xnMethod = doc.CreateElement(enumCodeShopType.Method.ToString());
                   xnMethod.SetAttribute("name", tlnMethod.GetValue("Name").ToString());
                   xnMethod.SetAttribute("returnType", tlnMethod.GetValue("ReturnType").ToString());
                   xnMethod.SetAttribute("parameters", tlnMethod.GetValue("Param").ToString());
                   xnMethod.SetAttribute("access", tlnMethod.GetValue("Accessibility").ToString());
                   xnMethod.InnerText = tlnMethod.GetValue("Code").ToString();

                   xnFolder.AppendChild(xnMethod);
               }

               xnRoot.AppendChild(xnFolder);
           }
       }

       public void LoadConfig(XmlDocument doc, TreeList tree)
       {
           foreach (XmlNode xnFolder in doc.SelectSingleNode("//Code").ChildNodes)
           {
               TreeListNode tlnFolder = tree.AppendNode(null, null);
               tlnFolder.SetValue("Name", xnFolder.Attributes["name"].Value);
               tlnFolder.SetValue("ReturnType", "");
               tlnFolder.SetValue("Param", "");
               tlnFolder.SetValue("Accessibility", "");
               tlnFolder.SetValue("Code", "");
               tlnFolder.SetValue("Type", enumCodeShopType.Folder);

               foreach (XmlNode xnMethod in xnFolder.ChildNodes)
               {
                   TreeListNode tlnMethod = tree.AppendNode(null, tlnFolder.Id);
                   tlnMethod.SetValue("Name", xnMethod.Attributes["name"].Value);
                   tlnMethod.SetValue("ReturnType", xnMethod.Attributes["returnType"].Value);
                   tlnMethod.SetValue("Param", xnMethod.Attributes["parameters"].Value);
                   tlnMethod.SetValue("Accessibility", xnMethod.Attributes["access"].Value);
                   tlnMethod.SetValue("Code", xnMethod.InnerText);
                   tlnMethod.SetValue("Type", enumCodeShopType.Method);
               }
           }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
       }  

    }
}
